﻿SELECT
FK_Schema = FK.TABLE_SCHEMA,
FK_Table = FK.TABLE_NAME,
FK_Column = CU.COLUMN_NAME,

PK_Schema = PK.TABLE_SCHEMA,
PK_Table = PK.TABLE_NAME,
--PK_Column = PT.COLUMN_NAME,

IX_Schema = IXS.name,
IX_Table = IXT.name,
IX_Column = IXCC.name,

Constraint_Name = C.CONSTRAINT_NAME,
Update_Action = rc.UPDATE_RULE,
Delete_Action = rc.DELETE_RULE,

o.object_id

FROM [SERVER].INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS C
INNER JOIN [SERVER].INFORMATION_SCHEMA.TABLE_CONSTRAINTS FK ON C.CONSTRAINT_NAME = FK.CONSTRAINT_NAME

LEFT JOIN [SERVER].INFORMATION_SCHEMA.TABLE_CONSTRAINTS PK ON C.UNIQUE_CONSTRAINT_NAME = PK.CONSTRAINT_NAME
LEFT JOIN [SERVER].INFORMATION_SCHEMA.KEY_COLUMN_USAGE CU ON C.CONSTRAINT_NAME = CU.CONSTRAINT_NAME
--LEFT JOIN (
--SELECT i1.TABLE_NAME, i2.COLUMN_NAME
--FROM [SERVER].INFORMATION_SCHEMA.TABLE_CONSTRAINTS i1
--INNER JOIN [SERVER].INFORMATION_SCHEMA.KEY_COLUMN_USAGE i2 ON i1.CONSTRAINT_NAME = i2.CONSTRAINT_NAME
--WHERE i1.CONSTRAINT_TYPE = 'PRIMARY KEY'
--) PT ON PT.TABLE_NAME = PK.TABLE_NAME
INNER JOIN [SERVER].INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS rc ON FK.CONSTRAINT_NAME = rc.CONSTRAINT_NAME

LEFT JOIN [SERVER].sys.indexes IX ON IX.name = C.UNIQUE_CONSTRAINT_NAME
LEFT JOIN [SERVER].sys.objects IXT ON IXT.object_id = IX.object_id
LEFT JOIN [SERVER].sys.index_columns IXC ON IX.index_id = IXC.index_id and IX.object_id = IXC.object_id
LEFT JOIN [SERVER].sys.columns IXCC ON IXCC.column_id = IXC.column_id AND IXCC.object_id = IXC.object_id
LEFT JOIN [SERVER].sys.schemas IXS ON IXT.schema_id = IXS.schema_id

inner join [SERVER].sys.objects o on FK.TABLE_NAME = o.name
inner join [SERVER].sys.schemas s on o.schema_id = s.schema_id and FK.TABLE_SCHEMA = s.name

where o.object_id =[OBJECT_ID_CONDITION]